package config

import (
	"context"
	"github.com/redis/go-redis/v9"
	"sync"
)

type connect struct {
	client *redis.Client
}

var once = sync.Once{}
var _connect *connect

func InitRedis() {

	ctx := context.Background()
	rdb := redis.NewClient(&redis.Options{
		Addr:     Config.Redis.Host + ":" + Config.Redis.Port,
		Password: Config.Redis.Password,
		DB:       0, // use default DB
	})
	err := rdb.Set(ctx, "go:hello", "init", 0).Err()
	if err != nil {
		panic(err)
	}
	_connect = &connect{
		client: rdb,
	}

}

func Client() *redis.Client {

	if _connect == nil {
		once.Do(func() {
			InitRedis()
		})
	}

	return _connect.client

}
